
package cz.czu.pef.DataModelling;

import java.util.*;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Index;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cz.czu.pef.DataModelling.utils.Lazy;

/**
 * This entity represents instances of given MedicalExaminationType for given related Person.
 * This means that given Person has passed given MedicalExaminationType.
 * 
 * @author rstribrn
 *
 */
@Entity
@Table(name = "MEDICAL_EXAMINATIONS")
@ManagedBean(name="medicalExamination")
@ViewScoped
@Component
@Scope(value="prototype")
public class MedicalExamination extends IdPersistenceObject
{
	private static final long serialVersionUID = -4318181001409286326L;

	/*
	 * Entity attributes
	 */
	@Column(nullable=false)
	@Index(name="IDX_MEDEXAM_LAST_PASS")
	private Date passedTimestamp;

	@ManyToOne(fetch = FetchType.EAGER, targetEntity=MedicalExaminationType.class, optional=false, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
	private MedicalExaminationType medicalExaminationType;

	/*
	 * Relations
	 */
	@ManyToMany(fetch = FetchType.LAZY, targetEntity = Person.class, mappedBy = "medicalExaminations", cascade=CascadeType.ALL) // all cascades!
	private Set<Person> persons;


	protected MedicalExamination()
	{
	}

	public MedicalExamination(MedicalExaminationType medType, Date passTimestamp)
	{
		setMedicalExaminationType(medType);
		setPassedTimestamp(passTimestamp);
	}

	@Lazy
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	public MedicalExaminationType getMedicalExaminationType()
	{
		return medicalExaminationType;
	}

	public void setMedicalExaminationType(MedicalExaminationType medicalExaminationType)
	{
		this.medicalExaminationType = medicalExaminationType;
	}


	public Date getExpiration()
	{
		// TODO: Compute expiration here from Employee => Roles => RoleType => 
		return null;
	}
	
	public Date getPassedTimestamp()
	{
		return passedTimestamp;
	}

	public void setPassedTimestamp(Date passedTimestamp)
	{
		this.passedTimestamp = passedTimestamp;
	}

	@Lazy
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	public Set<Person> getPersons()
	{
		return persons;
	}
	@Lazy
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	public List<Person> getPersonsAsList()
	{
		return new ArrayList<Person>(getPersons());
	}

	@Lazy
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	@Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("MedicalExamination [passedTimestamp=");
		builder.append(passedTimestamp);
		builder.append(", medicalExaminationType=");
		builder.append(medicalExaminationType);
		builder.append(", toString()=");
		builder.append(super.toString());
		builder.append("]");
		return builder.toString();
	}
	
	/**
	 * Can be deleted (in cascade).
	 */
	@Override
	public boolean canBeRemoved()
	{
		return true;
	}
}